home *** CD-ROM | disk | FTP | other *** search
/ FishMarket 1.0 / FishMarket v1.0.iso / fishies / 051-075 / disk_069 / spool / login.cx < prev    next >
Text File  |  1992-05-06  |  3KB  |  118 lines

  1. /* login.cx - log ports in and out */
  2.  
  3. /* this is an #included file !!!! */
  4.  
  5. /*
  6. **    8/18/86, Tim Holloway
  7. **
  8. **    Description: These routines are designed to ensure that a user does
  9. **    not find a message port, only to have it disappear before all
  10. **    messages have been transmitted to the destination port.  To that end,
  11. **    the sender must log in to the the destination port, send all messages
  12. **    and then log out.  The first byte of each message indicates log
  13. **    status.  If the receiver changes it to LOGOUT, the sender has been
  14. **    forced off the port and should send no further messages (including
  15. **    logout).
  16. **
  17. **    Caveats: The login ID for the sender is determined by the reply
  18. **    port address of the initial login.  You should either use the
  19. **    same message template for all further communications, or 'clone'
  20. **    the message.  That's the way Amiga EXEC I/O structures are handled,
  21. **    too!
  22. **
  23. **    Miscellaneous: A special data structure called a PORTPAIR is
  24. **    used in order to keep the sending and reply ports together.
  25. **    The PORTPAIR is initialized by LogInPort, and should not be
  26. **    modified by the user.
  27. */
  28.  
  29. enum portid {OUTPORT, INPORT};
  30. /* #define OUTPORT 0 / #define INPORT 1 ... if your compiler can't enum */
  31.  
  32. typedef struct MsgPort *PORT_ADDRESS;
  33. typedef PORT_ADDRESS PORTPAIR[2];
  34.  
  35. extern PORT_ADDRESS     CreatePort(char *, int);    /* prototypes */
  36. extern PORT_ADDRESS    FindPort (char *);
  37. extern VOID         DeletePort (PORT_ADDRESS);
  38.  
  39.  
  40. /*
  41. **    Log in a message port.  Assures that receiver will not delete port
  42. **    out from under us!  Creates a reply port. and initializes a portpair -
  43. **    i.e. array containing the addresses of the outbound and reply ports.
  44. */
  45.  
  46. BOOL
  47. LogInPort (port_name, ports, log_message)
  48. char *port_name;
  49. PORTPAIR ports;
  50. SPOOLmsg  *log_message;
  51. {
  52.     REGISTER PORT_ADDRESS mpin, mpout;
  53.  
  54. /*
  55.    Note - we build a reply port FIRST, even though we may not be able to
  56.    use it, simply because we don't want to do any more work while Forbidden
  57.    than absolutely neccessary.  I refuse to feel guilty about the xxx
  58.    microseconds of work wasted if the login failed.  Better that than
  59.    playing ping-pong with Forbids and Permits.  Two Permits for one Forbid
  60.    was bad enough!
  61. */
  62.  
  63.     mpin = ports[INPORT] = CreatePort (NULL, 0);    /* reply port */
  64.     if (mpin == NULL)
  65.     {
  66.         error ("Wasn't able to create the reply port.");
  67.         return FALSE;
  68.     }
  69.  
  70.     Forbid();    /* forbid other tasks from pre-empting */
  71.  
  72. /* printf ("Reply port is at %lx.\nFind port..", mpin); */
  73.  
  74.     mpout = ports[OUTPORT]    = FindPort(port_name);
  75.     if (mpout != NULL)
  76.     {
  77.         log_message->log_status = LOG_IN;
  78.         log_message->minfo.mn_ReplyPort = mpin;
  79.         PutMsg (mpout, log_message);
  80.  
  81. /* printf ("message sent.  Waiting.\n"); */
  82.  
  83.         (void) WaitPort(mpin);
  84.  
  85. /* printf ("operation complete.\n"); */
  86.  
  87.     Permit();    /* */
  88.         (void) GetMsg (mpin);
  89.         return ((BOOL) (log_message->log_status != LOG_OUT));
  90.     }
  91.  
  92.     Permit();    /* back to the multitasking world! */
  93.     DeletePort (mpin);
  94.     return FALSE;
  95. }
  96.  
  97. /* disconnect port pair and clean up */
  98.  
  99. static VOID
  100. LogOutPort(ports, log_message)
  101. PORTPAIR ports;
  102. SPOOLmsg  *log_message;
  103. {
  104.  
  105. /* printf ("log out ports\n"); */
  106.     if (log_message->log_status != LOG_OUT)
  107.     {
  108.         log_message->log_status = LOG_OUT;
  109.         log_message->minfo.mn_ReplyPort = ports[INPORT];
  110.         PutMsg (ports[OUTPORT], log_message);
  111.         (VOID) WaitPort (ports[INPORT]);
  112.         (VOID) GetMsg (ports[INPORT]);
  113.     }
  114.  
  115.     if (ports[INPORT] != NULL)
  116.         DeletePort (ports[INPORT]);    /* get rid of port we allocated */
  117. }
  118.